package com.gullmall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.gullmall.ware.entity.vo.MergeVo;
import com.gullmall.ware.entity.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gullmall.ware.entity.PurchaseEntity;
import com.gullmall.ware.service.PurchaseService;
import com.gullmall.common.utils.PageUtils;
import com.gullmall.common.utils.R;



/**
 * 采购信息
 *
 * @author lidai
 * @email 332617778@qq.com
 * @date 2022-05-19 17:00:30
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 领取采购单
     * @param purchaseIds  wms_purchase 中的ID
     */
    @PostMapping("/receive")
    public R receive(@RequestBody List<Long> purchaseIds){
        return purchaseService.receive(purchaseIds);
    }


    /**
     * 请求参数
     * {
     *    id: 123,//采购单id
     *    items: [{itemId:1,status:4,reason:""}]  完成(原因无需写) 或 失败的原因 (itemId是采购需求的id)
     *  }
     */
    @PostMapping("/done")
    public R done(@RequestBody PurchaseDoneVo purchaseDoneVo){
        return purchaseService.done(purchaseDoneVo);
    }

    /**
     * 查询未领取(新建或已分配)的采购单
     */
    @GetMapping("/unreceive/list")
    public R unReceiveList(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.unReceiveList(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase){
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/merge")
    public R mergePurchase(@RequestBody MergeVo mergeVo){
        return purchaseService.mergePurchase(mergeVo);
    }

}
